package com.yihukurama.sysbase.framework.domainservice;

import com.yihukurama.base.app.exception.TipsException;
import com.yihukurama.base.app.utils.EmptyUtil;
import com.yihukurama.base.app.utils.TransferUtils;
import com.yihukurama.sysbase.framework.domain.Role;
import com.yihukurama.sysbase.framework.domain.Roledept;
import com.yihukurama.sysbase.framework.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yihukurama.base.framework.service.domainservice.CrudService;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 说明： Role的领域服务
 *
 * @Author: yihukurama
 */
@Service
public class RoleService extends CrudService<RoleEntity> {


    @Autowired
    RolemenuService rolemenuService;

    @Autowired
    RoledeptService roledeptService;
    @Autowired
    MenuService menuService;

    @Autowired
    DeptService deptService;

    @Override
    @Transactional
    public RoleEntity create(RoleEntity roleEntity) throws TipsException {
        Role role = TransferUtils.transferEntity2Domain(roleEntity, Role.class);
        roleEntity = super.create(roleEntity);
        //创建角色与菜单权限的关联
        List<String> menuIds = role.getMenuIds();
        for (String menuId : menuIds
        ) {

            RolemenuEntity rolemenuEntity = new RolemenuEntity();
            rolemenuEntity.setRoleId(roleEntity.getId());
            rolemenuEntity.setMenuId(menuId);

            rolemenuService.create(rolemenuEntity);
        }

        return roleEntity;
    }

    @Override
    public RoleEntity load(RoleEntity roleEntity) throws TipsException {

        roleEntity = super.load(roleEntity);
        RolemenuEntity rolemenuEntity = new RolemenuEntity();
        rolemenuEntity.setRoleId(roleEntity.getId());
        List<RolemenuEntity> rolemenuEntityList = rolemenuService.list(rolemenuEntity);

        Role role = TransferUtils.transferEntity2Domain(roleEntity, Role.class);
        role.setRolemenuEntityList(rolemenuEntityList);

        List<MenuEntity> menuEntityList = new ArrayList<>();
        //加载菜单
        for (RolemenuEntity rolemenu : rolemenuEntityList
        ) {
            String menuId = rolemenu.getMenuId();
            MenuEntity menuEntity = new MenuEntity();
            menuEntity.setId(menuId);
            menuEntity = menuService.load(menuEntity);
            menuEntityList.add(menuEntity);
        }

        //加载部门
        RoledeptEntity roledeptEntity = new RoledeptEntity();
        roledeptEntity.setRoleId(roleEntity.getId());
        List<RoledeptEntity> roledeptEntityList = roledeptService.list(roledeptEntity);

        DeptEntity deptEntity = null;
        List<DeptEntity> deptEntityList = new ArrayList<>();
        for (RoledeptEntity roledept : roledeptEntityList
        ) {
            String deptId = roledept.getDeptId();
            deptEntity = new DeptEntity();
            deptEntity.setId(deptId);
            deptEntity = deptService.load(deptEntity);
            deptEntityList.add(deptEntity);
        }

        role.setMenuEntityList(menuEntityList);
        role.setDeptEntityList(deptEntityList);
        return role;
    }

    @Override
    public RoleEntity update(RoleEntity roleEntity) throws TipsException {
        Role role = TransferUtils.transferEntity2Domain(roleEntity, Role.class);


        List<String> deptIds = role.getDeptIds();
        RoledeptEntity roledeptEntity = new RoledeptEntity();
        if (!EmptyUtil.isEmpty(deptIds)) {
            List<String> roleDeptIds = new ArrayList<>();
            roledeptEntity.setRoleId(roleEntity.getId());
            List<RoledeptEntity> roledeptEntityList = roledeptService.list(roledeptEntity);
            for (RoledeptEntity temp : roledeptEntityList
            ) {
                roleDeptIds.add(temp.getId());
            }

            //批量删除原来的自定义数据权限
            roledeptEntity.setIds(roleDeptIds);
            try {
                roledeptService.removes(roledeptEntity);
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }

            //重新关联
            for (String deptId : deptIds
            ) {
                roledeptEntity = new RoledeptEntity();
                roledeptEntity.setRoleId(roleEntity.getId());
                roledeptEntity.setDeptId(deptId);
                roledeptService.create(roledeptEntity);
            }
        }


        List<String> menuIds = new ArrayList<>();
        RolemenuEntity rolemenuEntity = new RolemenuEntity();
        if (!EmptyUtil.isEmpty(menuIds)) {

            List<String> roleMenuIds = new ArrayList<>();

            rolemenuEntity.setRoleId(roleEntity.getId());
            List<RolemenuEntity> rolemenuEntityList = rolemenuService.list(rolemenuEntity);
            for (RolemenuEntity temp : rolemenuEntityList
            ) {
                roleMenuIds.add(temp.getId());
            }

            //批量删除原关联
            rolemenuEntity.setIds(roleMenuIds);
            try {
                rolemenuService.removes(rolemenuEntity);
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }


            //重新关联
            for (String menuId : menuIds
            ) {
                rolemenuEntity = new RolemenuEntity();
                rolemenuEntity.setRoleId(roleEntity.getId());
                rolemenuEntity.setMenuId(menuId);

                rolemenuService.create(rolemenuEntity);
            }

        }


        return super.update(roleEntity);
    }
}
